package dr.joketalker.controller;

import java.util.LinkedList;
import java.util.List;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
import dr.joketalker.R;
import dr.joketalker.account.AccountManager;
import dr.joketalker.controller.view.GridItem;
import dr.joketalker.controller.view.GridViewAdapter;
import dr.joketalker.dao.FactoryDAO;
import dr.joketalker.dao.JokeDAO;
import dr.joketalker.dao.JokeEvaluationDAO;
import dr.joketalker.entity.Joke;
import dr.joketalker.entity.JokeEvaluation;
import dr.joketalker.model.VoiceRecorderModel;
public class JokeController extends MyController {


	protected GridItem giPlay;
	protected GridItem giRate;
	
	protected GridView gdvControl;
    protected GridViewAdapter gdvAdapterControl = new GridViewAdapter(this);


	protected TextView tvTitle;
	protected TextView tvCategory;
	protected TextView tvLanguage;
	protected TextView tvCreated;
	protected TextView tvCreator;
	protected TextView tvDescription;

	protected TextView tvClicks;
	
	protected RatingBar rbRating;
	

	protected JokeEvaluationDAO jokeEvaluationDao = FactoryDAO.getJokeEvaluationDao();
	protected JokeDAO jokeDao = FactoryDAO.getJokeDao();
	
	protected List<TextView> listTextView = new LinkedList<TextView>();
	
	protected VoiceRecorderModel mModel = new VoiceRecorderModel();
	
	protected Joke joke;
	
	 @Override
	public void onCreate(Bundle savedInstanceState) {
	 
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.joke);
	        

	       // btnPlayFtp 		= (Button)findViewById(R.id.btnPlayFtp);
	        
	    	tvTitle			= (TextView)findViewById(R.id.tvTitle);
	    	tvCategory		= (TextView)findViewById(R.id.tvCategory);
	    	tvLanguage		= (TextView)findViewById(R.id.tvLanguage);
	    	tvCreated		= (TextView)findViewById(R.id.tvCreated);
	    	tvCreator		= (TextView)findViewById(R.id.tvCreator);
	    	tvDescription	= (TextView)findViewById(R.id.tvDescription);

	    	tvClicks		= (TextView)findViewById(R.id.tvClicks);
	    	
	    	rbRating 			= (RatingBar)findViewById(R.id.rbRating);


	    	listTextView.add(tvTitle);
	    	listTextView.add(tvCategory);
	    	listTextView.add(tvLanguage);
	    	listTextView.add(tvCreated);
	    	listTextView.add(tvCreator);
	    	listTextView.add(tvDescription);
	    	listTextView.add(tvClicks);
	    	
	    	//gdvControl=================================================
	        gdvControl = (GridView)findViewById(R.id.gdvControl);
	    	
	    	List<GridItem> listGridItem = new LinkedList<GridItem>();
	        giPlay = new GridItem(this,R.string.play,R.drawable.play);
	        listGridItem.add(giPlay);
	        
	        giRate = new GridItem(this,R.string.rate,R.drawable.star);
	        listGridItem.add(giRate);
        
	        for(GridItem gi : listGridItem){
	        	gdvAdapterControl.addItem(gi);
	        }

	        gdvControl.setAdapter(gdvAdapterControl);
	        gdvControl.setOnItemClickListener(this);
	        
	        //gdvControl=================================================
	        
	        Intent intent = getIntent();
	        this.joke = (Joke)intent.getSerializableExtra("JOKE");

	        clear();
	        init(joke);	        
	 }


	 public void init(Joke joke){
		 super.init();
		 this.joke 		= joke;
		 tvTitle		.setText(joke.getTitle());
		 tvCategory		.setText(joke.getCategory());
		 tvLanguage		.setText(joke.getLanguage());
		 tvCreated		.setText(joke.getCreated());
		 tvCreator		.setText(joke.getUser().getName());
		 tvDescription	.setText(joke.getDescription());
		 

		 
		 tvClicks.setText(String.valueOf(joke.getClicks()));
		 
		 
		 //rbEval.setRating(joke.getPoints());
		 
	 }
	 @Override
	public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) {
		// TODO Auto-generated method stub
		super.onItemClick(arg0, v, arg2, arg3);

		
		myDebug.v("wasn "+v);
		
		if(giPlay.equals(v)){
			playInYoutoube();
		}else if(giRate.equals(v)){

		}else{
			
//			Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(joke.getLinkFtp()));
//			startActivity(intent); 
		}
	}

	
	public void playInYoutoube(){
		
		joke.setClicks(joke.getClicks()+1);
		jokeDao.update(joke);
		
		
		Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(joke.getLinkYt()));
		startActivity(intent);	
	}
	
	public void playFromFtp(){
		//todo have to download it
	}
	
	
	public void clear(){
		for(TextView tv : listTextView){
			tv.setText("");
		}
	}
	
	public void eval(){
		JokeEvaluation jEval = new JokeEvaluation();
		jEval.setJoke(joke);
		jEval.setUser(AccountManager.getInstance().getLoggedUser());
		jEval.setPoints(rbRating.getRating());
	}



	
}
